package com.gdteam.kernel.domain.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.tapestry5.ioc.annotations.Inject;

import com.gdteam.kernel.domain.Domain;
import com.gdteam.kernel.domain.DomainModelFactory;
import com.gdteam.kernel.persistence.EntityProvider;

public class DomainModelFactoryImpl implements DomainModelFactory {

    Map<String, Class<? extends Domain>> dmos = new HashMap<String, Class<? extends Domain>>();
    
    @Inject
    public DomainModelFactoryImpl(EntityProvider provider) {
        for (Class<? extends Domain> cl : provider.getEntities()) {
            Domain dom;
            try {
                dom = cl.newInstance();
                dmos.put(dom.getFQN(), cl);
            } catch (InstantiationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    public Domain get(String fqn) {
        try {
            Class<? extends Domain> dmo = this.dmos.get(fqn);
            return dmo.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        
    }
    
}
